#include "StdAfx.h"
#include "CoordinationActor.h"
#include "Communication/CommunicationManager.h"
#include "SelectionTree/SelectionTree.h"
#include "SelectionTree/SelectionTreeTemplate.h"

#include "PipeUser.h"

EntityId CoordinationActor::GetEntityID() const
{
	if (CAIObject* object = m_object.GetAIObject())
		return object->GetEntityID();

	return 0;
}

CommConfigID CoordinationActor::GetCommunicationConfigID() const
{
	if (CAIObject* object = m_object.GetAIObject())
	{
		if (IAIActorProxy* proxy = object->GetProxy())
			return gAIEnv.pCommunicationManager->GetConfigID(proxy->GetCommunicationConfigName());
	}		
	
	return CommConfigID(0);
};

const char* CoordinationActor::GetName() const
{
	if (CAIObject* object = m_object.GetAIObject())
		return object->GetName();

	return "<null>";
}

IScriptTable* CoordinationActor::GetScript() const
{
	if (CAIObject* object = m_object.GetAIObject())
	{
		IEntity* entity = object->GetEntity();
		assert(entity);
		if(entity)
		{
			return entity->GetScriptTable();
		}
	}
	return 0;
}

void CoordinationActor::Signal(const char* signal) const
{
	CAIObject* object = m_object.GetAIObject();

	if (CAIActor* actor = object ? object->CastToCAIActor() : 0)
		actor->SetSignal(AISIGNAL_ALLOW_DUPLICATES, signal);
}

void CoordinationActor::SetBehaviorVariable(const char* variableName, bool val) const
{
	CAIObject* object = m_object.GetAIObject();

	if (CAIActor* actor = object ? object->CastToCAIActor() : 0)
		actor->SetBehaviorVariable(variableName,val);
}

bool CoordinationActor::SelectGoalPipe(int id, const char* name, int pipeID) const
{
	CAIObject* object = m_object.GetAIObject();

	if (CPipeUser* pipeuser = object ? object->CastToCPipeUser() : 0)
	{
		return pipeuser->SelectPipe(id, name, NILREF,pipeID);
	}

	return false;
}

void CoordinationActor::StartAction(const char* name) const
{
	CAIObject* object = m_object.GetAIObject();
	
	if (CAIActor* actor = object ? object->CastToCAIActor() : 0)
	{
		if (SelectionTree* tree = actor->GetBehaviorSelectionTree())
		{
			SelectionVariableID variableID = tree->GetTemplate().GetVariableDeclarations().GetVariableID(name);
			
			if (tree->GetTemplate().GetVariableDeclarations().IsDeclared(variableID))
			{
				if (SelectionVariables* variables = actor->GetBehaviorSelectionVariables())
					variables->SetVariable(variableID, true);
			}
		}
	}
}

void CoordinationActor::StopAction(const char* name) const
{
	CAIObject* object = m_object.GetAIObject();
	
	if (CAIActor* actor = object ? object->CastToCAIActor() : 0)
	{
		if (SelectionTree* tree = actor->GetBehaviorSelectionTree())
		{
			SelectionVariableID variableID = tree->GetTemplate().GetVariableDeclarations().GetVariableID(name);

			if (tree->GetTemplate().GetVariableDeclarations().IsDeclared(variableID))
			{
				if (SelectionVariables* variables = actor->GetBehaviorSelectionVariables())
					variables->SetVariable(variableID, false);
			}
		}
	}
}

void CoordinationActor::RegisterGoalPipeListener(IGoalPipeListener* listener, int pipeID) const
{
	CAIObject* object = m_object.GetAIObject();

	if (CPipeUser* pipeuser = object ? object->CastToCPipeUser() : 0)
		pipeuser->RegisterGoalPipeListener(listener, pipeID, "CoordinationActor::RegisterGoalPipeListener");
}

void CoordinationActor::UnregisterGoalPipeListener(IGoalPipeListener* listener, int pipeID) const
{
	CAIObject* object = m_object.GetAIObject();

	if (CPipeUser* pipeuser = object ? object->CastToCPipeUser() : 0)
		pipeuser->UnRegisterGoalPipeListener(listener, pipeID);
}

void CoordinationActor::RegisterBehaviorListener(IActorBehaviorListener* listener) const
{
	CAIObject* object = m_object.GetAIObject();

	if (CAIActor* actor = object ? object->CastToCAIActor() : 0)
		actor->RegisterBehaviorListener(listener);
}

void CoordinationActor::UnregisterBehaviorListener(IActorBehaviorListener* listener) const
{
	CAIObject* object = m_object.GetAIObject();

	if (CAIActor* actor = object ? object->CastToCAIActor() : 0)
		actor->UnregisterBehaviorListener(listener);
}

void CoordinationActor::NotifyPerforming(const char* roleName) const
{
	CAIObject* object = m_object.GetAIObject();

	if (CAIActor* actor = object ? object->CastToCAIActor() : 0)
		actor->CoordinationEntered("PerformingRole");
		//actor->SetSignal(AISIGNAL_ALLOW_DUPLICATES, "PerformingRole");
}

void CoordinationActor::NotifyFinished(const char* roleName) const
{
	CAIObject* object = m_object.GetAIObject();

	if (CAIActor* actor = object ? object->CastToCAIActor() : 0)
		actor->CoordinationExited("RoleFinished");
		//actor->SetSignal(AISIGNAL_ALLOW_DUPLICATES, "RoleFinished");
}

void CoordinationActor::NotifyFailed(const char* roleName) const
{
	CAIObject* object = m_object.GetAIObject();

	if (CAIActor* actor = object ? object->CastToCAIActor() : 0)
		actor->CoordinationExited("RoleFailed");
		//actor->SetSignal(AISIGNAL_ALLOW_DUPLICATES, "RoleFailed");
}